﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics;

namespace LowLevelGraphics.Filter
{ 
    public class Pixel : IEnumerator, IEnumerable
    {
        private static UnsafeBitmap bitmap = null;
        private static Neighbours aNeighbours = null;
        private static int nWidth = -1;
        private static int nHeight = -1;
        private int m_nX = -1;
        private int m_nY = -1;
        private int m_nIndex = -1;

        public static UnsafeBitmap Bitmap
        {
            set
            {
                bitmap = value;
                nWidth = bitmap.Width;
                nHeight = bitmap.Height;
            }
            get { return bitmap; }
        }

        public Pixel(int _nX, int _nY)
        {
            m_nX = _nX;
            m_nY = _nY;
        }

        public Color Color
        {
            get 
            {
                if (m_nX > -1 && m_nX < nWidth && m_nY > -1 && m_nY < nHeight)
                    return bitmap.GetPixel(m_nX, m_nY);
                else
                    return Color.Transparent;
            }
            set
            {
                if (m_nX > -1 && m_nX < nWidth && m_nY > -1 && m_nY < nHeight)
                    bitmap.SetPixel(m_nX, m_nY, value);
            }
        }

        public Color this[int _nXOffset, int _nYOffset]
        {
            get {
                int nTempX = m_nX + _nXOffset;
                int nTempY = m_nY + _nYOffset;
                
                if (nTempX > -1 && nTempX< nWidth && nTempY > -1 && nTempY < nHeight)
                    return bitmap.GetPixel(nTempX, nTempY);
                else
                    return Color.Transparent;
            }
            set {
                int nTempX = m_nX + _nXOffset;
                int nTempY = m_nY + _nYOffset;
                if (nTempX > -1 && nTempX < nWidth - 1 && nTempY > -1 && nTempY < nHeight)
                    bitmap.SetPixel(nTempX, nTempY, value);
            }
        }

        public Color this[Neighbour _neighbour]
        {
            get { return this[_neighbour.XOffset, _neighbour.YOffset]; }
            set { this[_neighbour.XOffset, _neighbour.YOffset] = value; }
        }

        public int X
        {
            get { return m_nX; }
            set { m_nX = value; }
        }

        public int y
        {
            get { return m_nY; }
            set { m_nY = value; }
        }

        #region IEnumerator<Neighbours> Members

        public static void SetNeighbours(Neighbours _aNeighbour)
        {
            aNeighbours = _aNeighbour;
        }

        private static Neighbour aNeighbours1 = null;

        public Neighbour Current
        {
            get
            {
                aNeighbours1 = aNeighbours[m_nIndex];
                aNeighbours1.Pixel = this;
                return aNeighbours1;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { return this.Current; }
        }

        public bool MoveNext()
        {
            if (m_nIndex + 1 < aNeighbours.Count)
            {
                m_nIndex++;
                return true;
            }
            else
            {
                return false;
            }
       }

        public void Reset()
        {
            m_nIndex = -1;
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator Neighbours
        {
            get { return this; }
        }

        public IEnumerator GetEnumerator()
        {
            return this;
        }

        #endregion   
    }
}
